package com.deer.wms.ASN.manage.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.constant.AsnManageConstant;
import com.deer.wms.ASN.manage.model.asn.AsnMasterDto;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.model.qc.QcTaskCriteria;
import com.deer.wms.ASN.manage.model.qc.QcTaskView;
import com.deer.wms.ASN.manage.service.QcTaskService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.StringUtil;
import com.deer.wms.project.root.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : wei
 * @since : 2022-01-13 09:51:41
 **/
@Api(description = "质检任务池api接口")
@RestController
@RequestMapping("/qc/task")
public class QcTaskController {
    @Autowired
    private QcTaskService qcTaskService;

    @GetMapping("/list")
    public Result list(QcTaskCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        QueryWrapper<QcTaskView> queryWrapper =new QueryWrapper<>();
        if(StringUtils.isNotEmpty(criteria.getAsnMasterCode())){
            queryWrapper.eq("asn_bill_no",criteria.getAsnMasterCode());
        }
        if(StringUtils.isNotEmpty(criteria.getItemCode())){
            queryWrapper.like("item_code",criteria.getItemCode());
        }
        if(StringUtils.isNotEmpty(criteria.getBatchName())){
            queryWrapper.like("batch_name",criteria.getBatchName());
        }
        if(StringUtils.isNotEmpty(criteria.getItemName())){
            queryWrapper.like("item_name",criteria.getItemName());
        }
        if(StringUtils.isNotEmpty(criteria.getAcceptRecordCode())){
            queryWrapper.like("accept_record_code",criteria.getAcceptRecordCode());
        }
        List<QcTaskView> list = qcTaskService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "根据收货单号查询单条待检任务")
    @GetMapping("/info")
    public Result qcInfo(String acceptRecordCode) {
        QcTaskView qcTask = qcTaskService.getOneByField("accept_record_code", acceptRecordCode);
        if (ObjectUtil.isNull(qcTask)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "该收货单下无待检数据");
        }
        return ResultGenerator.genSuccessResult(qcTask);
    }

}
